#pragma once
#include <iostream>
#include <string>
#include <cstdio>
#include <ctime>
#include <unistd.h>
#include <cstdarg>

#define DEBUG 0
#define NORMAL 1
#define WARNING 2
#define ERROR 3
#define FATAL 4 // 致命

#define NUM 1024
#define LogErr "logErr.txt"
#define LogNORMAL "logNormal.txt"

const char *to_Level(int level)
{
    switch (level)
    {
    case DEBUG:
        return "DEBUG";
    case NORMAL:
        return "NORMAL";
    case WARNING:
        return "WARNING";
    case ERROR:
        return "ERROR";
    case FATAL:
        return "FATAL";
    default:
        return nullptr;
    }
}

void logMessage(int level, const char *format, ...) // 可变参数
{
    // [日志等级][时间][pid][format]
    // std::cout << message << std::endl;

    //[日志等级][时间][pid]
    char logprefix[NUM];
    snprintf(logprefix, sizeof(logprefix), "[%s][%ld][pid#-> %d]", to_Level(level), (long int)time(nullptr), getpid());

    //[format]
    char logpostfix[NUM];
    va_list arg; // 像指针一样
    va_start(arg, format);
    vsnprintf(logpostfix, sizeof(logpostfix), format, arg);

    std::cout << logprefix << " " << logpostfix << std::endl;
}
